package com.olap.starter.common.service.crud.impl.builder;

import com.baomidou.mybatisplus.annotation.DbType;
import com.olap.starter.common.service.crud.impl.builder.CRUDServiceTableConfigBuilder;
import com.olap.starter.common.service.crud.impl.builder.CRUDServiceTreeConfigBuilder;
import com.olap.starter.common.util.DBType;
import com.olap.starter.common.util.OnlineUtils;
import com.olap.starter.common.util.SqlUtils;
import com.olap.starter.common.vo.*;
import com.olap.starter.config.dataSource.JdbcTemplateSource;
import com.olap.starter.mapper.TableConfigMapper;
import com.olap.starter.pojo.TableConfig;
import com.olap.starter.pojo.TableDelete;
import com.olap.starter.service.TableDeleteServices;
import com.olap.starter.util.StringUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Log4j2
public class CRUDServiceBuilder {

    @Autowired
    private CRUDServiceTableConfigBuilder crudServiceTableConfigBuilder;
    @Autowired
    private CRUDServiceTreeConfigBuilder crudServiceTreeConfigBuilder;
    @Autowired
    private JdbcTemplateSource jdbcTemplateSource;

    @Autowired
    private TableDeleteServices tableDeleteServices;
    @Autowired
    private TableConfigMapper tableConfigMapper;

    public JdbcTemplate init(QueryParameter queryParameter) {
        if (queryParameter.getQueryType() == 1) {
            crudServiceTreeConfigBuilder.execute(queryParameter);
        } else {
            crudServiceTableConfigBuilder.execute(queryParameter);
        }
        JdbcTemplate jdbcTemplate = jdbcTemplateSource.getJdbcTemplate(queryParameter.getDbName());
        queryParameter.setDbType(DBType.getType(jdbcTemplate));
        return jdbcTemplate;
    }

    public JdbcTemplate init(BaseParameter baseParameter) {
        if (StringUtils.isBlank(baseParameter.getDbName()) || StringUtils.isBlank(baseParameter.getTableName())) {
            TableConfig tableConfig = tableConfigMapper.getTableConfigAndDbNameById(baseParameter.getTableConfigId());
            if(tableConfig == null){
                throw new RuntimeException("必填tableName|dbName!");
            }
            baseParameter.setDbName(tableConfig.getDbName());
            baseParameter.setTableName(tableConfig.getMainTable());
        }
        JdbcTemplate jdbcTemplate = jdbcTemplateSource.getJdbcTemplate(baseParameter.getDbName());
        baseParameter.setDbType(SqlUtils.getDbType(jdbcTemplate));
        return jdbcTemplate;
    }

    public TableDelete getTableDelete(Long parentId) {
        TableDelete tableDelete = tableDeleteServices.getBaseMapper().getTableDeleteAndDbName(parentId);
        if (tableDelete == null) {
            throw new RuntimeException("无效的$parentId！");
        }
        return tableDelete;
    }

    public JdbcTemplate getJdbcTemplate(String dbName) {
        return jdbcTemplateSource.getJdbcTemplate(dbName);
    }

    public JdbcTemplate getJdbcTemplate(BaseParameter parameter) {
        if(parameter.getDbName() == null){
            throw new RuntimeException("dbName必填！");
        }
        return jdbcTemplateSource.getJdbcTemplate(parameter.getDbName());
    }

    public boolean isDelete(String dbName, String tableName) {
        JdbcTemplate jdbcTemplate = getJdbcTemplate(dbName);
        if (DbType.MYSQL.getDb().toLowerCase().equals(SqlUtils.getDbType(jdbcTemplate).toLowerCase())) {
            String sql = "SELECT COUNT(*) as cunt FROM information_schema.columns WHERE table_schema = '" + dbName + "' AND table_name = '" + tableName + "' AND column_name = 'deleted'";
            Map<String, Object> map1 = jdbcTemplate.queryForMap(sql);
            return Integer.parseInt(map1.get("cunt").toString()) == 0;
        }
        return true;
    }

    public Object[] getMapObjectArrays(Map<String, Object> map, Map<String, Object> filedMap) {
        Object[] objects = new Object[filedMap.size()];
        int index = 0;
        for (String k : filedMap.keySet()) {
            String fk = OnlineUtils.getFontFiled(filedMap.get(k).toString());
            Object o = map.get(fk);
            if (o == null) {
                throw new RuntimeException("key条件[" + fk + "]不能为空！");
            }
            objects[index] = map.get(fk);
            index++;
        }
        return objects;
    }
}
